﻿using Domain.Entity;
using DtoFw;
using DtoFw.Common.Dto;
using DtoFw.Common.Query;
using DtoFw.Query;
using Repository.Base;
using System.Collections.Generic;
using Util;
using System.Linq;

namespace Repository
{
    public class MainOrganRepository : BaseRepository<MainOrganEntity>
    {
        private readonly SnowFlakeGenerator worker;
        public MainOrganRepository(SnowFlakeGenerator snowFlakeGenerator) : base(snowFlakeGenerator)
        {
            this.worker = snowFlakeGenerator;
        }
        //分页
        public Pager<MainOrganEntity> GetPager(OrganQuery query)
        {
            var organids = QueryOrgs(query.OrganId);
            return base.GetPager(query, linq =>
            {
                if (!string.IsNullOrEmpty(query.Name))
                    linq = linq.Where(t => t.CName.Contains(query.Name));
                if (!string.IsNullOrEmpty(query.Code))
                    linq = linq.Where(t => t.Code.Contains(query.Code));
                if (query.Id > 0)
                    linq = linq.Where(x => x.Id == query.Id);
                if (query.PId > 0)
                    linq = linq.Where(x => x.SuperiorId == query.PId || x.Id == query.PId);
                linq.Where(v => organids.Contains(v.Id));
                return linq;
            }, null, x => x.OrderBy,SqlSugar.OrderByType.Asc);
        }

        /// <summary>
        /// 获得当前单位以及下级所有单位
        /// </summary>
        /// <param name="query"></param>
        public OrganTreeDto GetAllOrgan(CommonQuery query)
        {
            var list = base.GetDb().Queryable<MainOrganEntity>().Where(x => !x.IsDeleted).ToList();
            //当前单位
            var myOrgan = list.Where(x => x.Id == query.OrganId).First();

            var tree = new OrganTreeDto
            {
                Id = myOrgan.Id,
                label = myOrgan.Name,
                Name = myOrgan.Name,
                Value = myOrgan.Id.ToString(),
                SuperiorId = 0,
                Type = 0,
                Icon = "",
                children = null,
            };

            GetChildOrgan(query.OrganId, list, tree);

            return tree;
        }

        /// <summary>
        /// 获得子集
        /// </summary>
        /// <param name="id"></param>
        /// <param name="linq"></param>
        /// <param name="tree"></param>
        private void GetChildOrgan(long id, List<MainOrganEntity> list, OrganTreeDto tree)
        {
            var preOrgan = list.Where(x => x.SuperiorId == id).ToList();
            foreach (var item in preOrgan)
            {
                var childtree = new OrganTreeDto
                {
                    Id = item.Id,
                    label = item.Name,
                    Name = item.Name,
                    Value = item.Id.ToString(),
                    SuperiorId = 0,
                    Type = 0,
                    Icon = "",
                    children = null,
                };

                GetChildOrgan(item.Id, list, childtree);
                if (tree.children == null)
                    tree.children = new List<object>();

                tree.children.Add(childtree);
            }
        }
    }
}
